<template>
  <div class="form-widget-container">
  
      <el-switch
            v-model="showTable"
            size="large"
            active-text="显示"
            inactive-text="编辑"
          />
      <div v-if="showTable">
         <el-table :data="tableData" style="width: 100%">
          <el-table-column v-for="item in designer.tableCloumnList" prop="date" :label="item.name" width="180" />
          
        </el-table>
      </div>
      <div v-else>
          <div>表格操作栏</div>
          
          <VTableToolBarWidget :designer="designer" :form-config="designer.formConfig" :global-dsv="globalDsv" ref="formRef">
          </VTableToolBarWidget>
         
          <el-row :gutter="20">
            <el-col :span="12">表单属性</el-col>
            <el-col :span="12">字段名称</el-col>
          </el-row>
          <draggable 
            :list="designer.tableCloumnList"
            ghost-class="ghost"
            handle=".move"
            filter=".forbid"
            :force-fallback="true"
            chosen-class="chosenClass"
            animation="300"
            @start="onStart"
            @end="onEnd"
            group="itxst"
            :fallback-class="true"
            :fallback-on-body="true"
            :touch-start-threshold="50"
            :fallback-tolerance="50"
            :move="onMove"
          >
            <template #item="{element }">
                <div class="item move">
                  <el-row :gutter="20" style="margin: 10px 0">
                    <el-col :span="10">
                        <el-select v-model="value" class="m-2" placeholder="Select" size="large">
                          <el-option
                            v-for="item in fieldList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                          />
                        </el-select>
                    </el-col>
                    <el-col :span="10"><el-input v-model="input" placeholder="Please input" /></el-col>
                    <el-col :span="4">
                        <el-button type="success" circle @click="addCloumn">+</el-button>
                        <el-button type="danger" circle  @click="removeCloume(element)">-</el-button>
                    </el-col>
                  </el-row>
                </div>
            </template>
        </draggable>
      </div>
  </div>
</template>

<script>
  import VTableToolBarWidget from './table-toolBar-widget/index'
  import i18n from "@/utils/i18n"
  
  export default {
    name: "VFormWidget",
    componentName: "VFormWidget",
    mixins: [i18n],
    components: {
      VTableToolBarWidget
    },
    props: {
      designer: Object,
      formConfig: Object,
      optionData: { //prop传入的选项数据
        type: Object,
        default: () => ({})
      },
      globalDsv: {
        type: Object,
        default: () => ({})
      },
    },
    provide() {
      return {
        refList: this.widgetRefList,
        getFormConfig: () => this.formConfig,  /* 解决provide传递formConfig属性的响应式更新问题！！ */
        getGlobalDsv: () => this.globalDsv, // 全局数据源变量
        globalOptionData: this.optionData,
        getOptionData: () => this.optionData,
        getReadMode: () => false,
        globalModel: {
          formModel: this.formModel,
        },
        getSubFormFieldFlag: () => false,
        getSubFormName: () => '',
        getDSResultCache: () => this.dsResultCache,
      }
    },
    inject: ['getDesignerConfig'],
    data() {
      return {
        showTable:true,
        formModel: {},
        widgetRefList: {},
        dsResultCache: {},
      }
    },
    computed: {
      labelPosition() {
        if (!!this.designer.formConfig && !!this.designer.formConfig.labelPosition) {
          return this.designer.formConfig.labelPosition
        }

        return 'left'
      },

      size() {
        if (!!this.designer.formConfig && !!this.designer.formConfig.size) {
          return this.designer.formConfig.size
        }

        return 'default'
      },

      customClass() {
        return this.designer.formConfig.customClass || ''
      },

      layoutType() {
        return this.designer.getLayoutType()
      },

      canvasMinHeight() {
        return (this.getDesignerConfig().logoHeader !== false) ? 'calc(100vh - 56px - 68px)' : 'calc(100vh - 56px - 68px + 48px)'
      }

    },
    watch: {
      'designer.widgetList': {
        deep: true,
        handler(val) {
          console.log(val)
        }
      },

      'designer.formConfig': {
        deep: true,
        handler(val) {
          //
        }
      },

    },
    created() {
      //this.designer.initDesigner( !!this.getDesignerConfig().resetFormJson )  //此行代码已移动到form-designer，以便提前赋值formConfig！！
      this.designer.loadPresetCssCode( this.getDesignerConfig().presetCssCode )
    },
    mounted() {
      this.disableFirefoxDefaultDrop()  /* 禁用Firefox默认拖拽搜索功能!! */
      this.designer.registerFormWidget(this)
    },
    methods: {
      
      disableFirefoxDefaultDrop() {
        let isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox") !== -1)
        if (isFirefox) {
          document.body.ondrop = function (event) {
            event.stopPropagation();
            event.preventDefault();
          }
        }
      },

      addCloumn(){
        this.designer.tableCloumnList.push(
          {id:"3",name:"列demo3",attr:""}
        );
      },

      removeCloume(element){
         let newList=this.designer.tableCloumnList.filter((e) => e.id != element.id);
         this.designer.tableCloumnList=newList;
      }
     

    }
  }
</script>

<style lang="scss" scoped>
  .container-scroll-bar {
    :deep(.el-scrollbar__wrap), :deep(.el-scrollbar__view) {
      overflow-x: hidden;
    }
  }

  .form-widget-container {
    padding: 10px;
    background: #f1f2f3;

    overflow-x: hidden;
    overflow-y: auto;

    .el-form.full-height-width {
      height: 100%;
      padding: 3px;
      background: #ffffff;

      .no-widget-hint {
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
        font-size: 18px;
        color: #999999;
      }

      .form-widget-canvas {
        //min-height: calc(100vh - 56px - 68px + 48px);
        padding: 3px;
      }
    }

    /* 隐藏组件拖拽状态中显示的黑点 */
    :deep(li.container-widget-item), :deep(li.field-widget-item) {
      list-style: none;
    }

    .el-form.PC-layout {
      //
    }

    .el-form.Pad-layout {
      margin: 0 auto;
      max-width: 960px;
      border-radius: 15px;
      box-shadow: 0 0 1px 10px #495060;
    }

    .el-form.H5-layout {
      margin: 0 auto;
      width: 420px;
      border-radius: 15px;
      //border-width: 10px;
      box-shadow: 0 0 1px 10px #495060;
    }

    .el-form.widget-form :deep(.el-row) {
      padding: 2px;
      border: 1px dashed rgba(170, 170, 170, 0.75);
    }
  }

  .grid-cell {
    min-height: 30px;
    border-right: 1px dotted #cccccc;

  }

  .fade-enter-active, .fade-leave-active {
    transition: opacity .5s;
  }

  .fade-enter, .fade-leave-to {
    opacity: 0;
  }
</style>
